﻿//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------

using GameFramework.Event;
using System;
using System.Threading.Tasks;
using UnityEngine;
using UnityGameFramework.Runtime;
using ProcedureOwner = GameFramework.Fsm.IFsm<GameFramework.Procedure.IProcedureManager>;

namespace Adventure
{
    public class ProcedureMain : ProcedureBase
    {
   
        private bool m_GotoMenu = false;
     
        private ControllerForm m_ControllerForm;

        public override bool UseNativeDialog
        {
            get
            {
                return false;
            }
        }

        private int MapEntityId { set; get; }
        //private int MapTypeId { set; get; }
        public void GotoMenu()
        {
            m_GotoMenu = true;
        }

        protected override void OnInit(ProcedureOwner procedureOwner)
        {
            base.OnInit(procedureOwner);

         
        }

        protected override void OnDestroy(ProcedureOwner procedureOwner)
        {
            base.OnDestroy(procedureOwner);

           
        }

        protected override void OnEnter(ProcedureOwner procedureOwner)
        {
            base.OnEnter(procedureOwner);

            GameEntry.Event.Subscribe(OpenUIFormSuccessEventArgs.EventId, OnOpenUIFormSuccess);
            GameEntry.Event.Subscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess);
       
            GameEntry.UI.OpenUIForm(UIFormId.ControllerForm,this);
            m_GotoMenu = false;

            GameEntry.TimerComponent.AddOnceTimer(100,()=> 
            { 
                ShowMap(int.Parse((VarString)GameEntry.DataNode.GetData(Constant.MenuFormMapId))); 
            });
        }


        protected override void OnLeave(ProcedureOwner procedureOwner, bool isShutdown)
        {       
            base.OnLeave(procedureOwner, isShutdown);

            GameEntry.Event.Unsubscribe(OpenUIFormSuccessEventArgs.EventId, OnOpenUIFormSuccess);
            GameEntry.Event.Unsubscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess);

            if (m_ControllerForm != null)
            {
                m_ControllerForm.Close(isShutdown);
                m_ControllerForm = null;
            }
        }

        protected override void OnUpdate(ProcedureOwner procedureOwner, float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);

            if (m_GotoMenu)
            {
               
                procedureOwner.SetData<VarInt32>(Constant.NextSceneId, GameEntry.Config.GetInt(Constant.SceneMenu));
                ChangeState<ProcedureChangeScene>(procedureOwner);
            }          
        }

        private void OnOpenUIFormSuccess(object sender, GameEventArgs e)
        {
            OpenUIFormSuccessEventArgs ne = (OpenUIFormSuccessEventArgs)e;
            if (ne.UserData != this)
            {
                return;
            }

            m_ControllerForm = (ControllerForm)ne.UIForm.Logic;
        }
        private void OnShowEntitySuccess(object sender, GameEventArgs e)
        {
            ShowEntitySuccessEventArgs ne = (ShowEntitySuccessEventArgs)e;

            if (ne.Entity.Id == MapEntityId)
            {
                ShowPlayer();
                ShowEnemy();
            }
        }
        public void ShowMap(int mapTypeId)
        {         
            MapEntityId = GameEntry.Entity.GenerateSerialId();

            
            GameEntry.Entity.ShowMap(new MapData(MapEntityId, mapTypeId) 
            {       
                Position = Vector3.zero 
            });
        }

        public void ShowPlayer()
        {
            Map map = GameEntry.Entity.GetEntity(MapEntityId).GetComponent<Map>();


            PlayerData playerData = GameEntry.ArchiveData.Data.player;
            DRPlayer dRPlayer = GameEntry.DataTable.GetDRPlayer(playerData.PlayerId);

            GameEntry.Entity.ShowPlayer(new PlayerEntityData(GameEntry.Entity.GenerateSerialId(), 10000)
            {
                Level = dRPlayer.Level,
                Attack = dRPlayer.Attack,
                Defense = dRPlayer.Defense,
                MaxHP = dRPlayer.MaxHP,
                NeedEXP = dRPlayer.MaxEXP,

                CurrentHP = playerData.CurrentHP,
                CurrentEXP = playerData.CurrentEXP,

                Position = map.playerPoint,
            });

        }
        public /*async*/ void ShowEnemy()
        {
            Map map = GameEntry.Entity.GetEntity(MapEntityId).GetComponent<Map>();

            for (int i = 0; i < map.GetEnemyInfo()[Map.EnemyType.野猪].Count; i++)
            {

                var enemyData = GameEntry.DataTable.GetDREnemy(map.EnemyTipeId_01[i]);
                GameEntry.Entity.ShowEnemy(new EnemyEntityData(GameEntry.Entity.GenerateSerialId(), 10001)
                {
                    Level = enemyData.Level,
                    Attack = enemyData.Attack,
                    Defense = enemyData.Defense,
                    MaxHP = enemyData.MaxHP,
                    CurrentHP = enemyData.MaxHP,
                    SelfEXP = enemyData.EXP,

                    Position = map.GetEnemyInfo()[Map.EnemyType.野猪][i],

                });
                //await Task.Delay((int)( 1000));
            }

            for (int i = 0; i < map.GetEnemyInfo()[Map.EnemyType.蜗牛].Count; i++)
            {
                var enemyData = GameEntry.DataTable.GetDREnemy(map.EnemyTipeId_02[i]);
                GameEntry.Entity.ShowEnemy(new EnemyEntityData(GameEntry.Entity.GenerateSerialId(), 10002)
                {
                    Level = enemyData.Level,
                    Attack = enemyData.Attack,
                    Defense = enemyData.Defense,
                    MaxHP = enemyData.MaxHP,
                    CurrentHP = enemyData.MaxHP,
                    SelfEXP = enemyData.EXP,

                    Position = map.GetEnemyInfo()[Map.EnemyType.蜗牛][i],

                });
            }

            for (int i = 0; i < map.GetEnemyInfo()[Map.EnemyType.蜜蜂].Count; i++)
            {
                var enemyData = GameEntry.DataTable.GetDREnemy(map.EnemyTipeId_03[i]);

                GameEntry.Entity.ShowEnemy(new EnemyEntityData(GameEntry.Entity.GenerateSerialId(), 10003)
                {
                    Level = enemyData.Level,
                    Attack = enemyData.Attack,
                    Defense = enemyData.Defense,
                    CurrentHP = enemyData.MaxHP,
                    MaxHP = enemyData.MaxHP,
                    SelfEXP = enemyData.EXP,

                    Position = map.GetEnemyInfo()[Map.EnemyType.蜜蜂][i],

                });
            }
        }
    }
}
